package lk.icta.common.util;

import lk.icta.common.AppConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;

public class CivilStatusUtil {
    private static final Logger logger = LoggerFactory.getLogger(CivilStatusUtil.class);

    /**
     * The civil status
     */
    public enum CivilStatus {
        NEVER_MARRIED   /** 0 - Never known to be married */
        ,
        MARRIED         /** 1 - Currently married */
        ,
        ANNULLED        /** 2 - event never happened */
        ,
        SEPARATED       /** 3 - Living separately from spouse. Cannot re-marry - TODO is this necessary? */
        ,
        DIVORCED        /** 4 - Legally divorced from the spouse. Can re-marry */
        ,
        WIDOWED         /** 5 - Spouse has died */
    }

    public static String getStatusAsString(Integer s) {
        if (s == null) {
            return "U"; // unknown
        }

        switch (s) {
            case 0:
                return "N";
            case 1:
                return "M";
            case 2:
                return "A";
            case 3:
                return "S";
            case 4:
                return "D";
            case 5:
                return "W";
        }
        throw new IllegalArgumentException("Illegal civil status : " + s);
    }

    public static String getStatusAsString(CivilStatus s) {
        if (s == null) {
            return "U"; // unknown
        }

        switch (s) {
            case NEVER_MARRIED:
                return "N";
            case MARRIED:
                return "M";
            case ANNULLED:
                return "A";
            case SEPARATED:
                return "S";
            case DIVORCED:
                return "D";
            case WIDOWED:
                return "W";
        }
        throw new IllegalArgumentException("Illegal civil status : " + s);
    }

    public static String getCivilStatus(CivilStatus state, String language) {

        if (AppConstants.SINHALA.equals(language)) {
            switch (state) {
                case NEVER_MARRIED:
                    return "???????";
                case MARRIED:
                    return "??????";
                case ANNULLED:
                    return "?????? ?? ??";
                case SEPARATED:
                    return "?????? ?? ";
                case DIVORCED:
                    return "???????? ?? ?? ";
                case WIDOWED:
                    return "???????";
            }
        } else if (AppConstants.TAMIL.equals(language)) {
            switch (state) {
                case NEVER_MARRIED:
                    return "??????????????";
                case MARRIED:
                    return "???????????? ";
                case ANNULLED:
                    return "????????????";
                case SEPARATED:
                    return "???????????????";
                case DIVORCED:
                    return "???????? ???????? ????????";
                case WIDOWED:
                    return "?????";
            }
        } else if (AppConstants.ENGLISH.equals(language)) {
            switch (state) {
                case NEVER_MARRIED:
                    return "NEVER MARRIED";
                case MARRIED:
                    return "MARRIED";
                case ANNULLED:
                    return "ANNULLED";
                case SEPARATED:
                    return "SEPARATED";
                case DIVORCED:
                    return "DIVORCED";
                case WIDOWED:
                    return "WIDOWED";
            }
        } else {
            logger.error("Invalid language : {}", language);
            throw new IllegalArgumentException("Invalid language : " + language);
        }
        logger.error("Invalid civil status : {}", state);
        throw new IllegalArgumentException("Invalid civil status : " + state);
    }

    public static String getCivilStatusInAllLanguages(CivilStatus state) {
        return getCivilStatus(state, AppConstants.SINHALA) + " / " + getCivilStatus(state, AppConstants.TAMIL) + " / " + getCivilStatus(state, AppConstants.ENGLISH);
    }

    /**
     * Populate civil status (Except MARRIED status) list with its values in all languages
     */
    public static Map<CivilStatus, String> populateCivilStatus() {
        Map<CivilStatus, String> civilStatus = new LinkedHashMap<CivilStatus, String>();
        civilStatus.put(CivilStatus.NEVER_MARRIED, CivilStatusUtil.getCivilStatusInAllLanguages(CivilStatus.NEVER_MARRIED));
        civilStatus.put(CivilStatus.DIVORCED, CivilStatusUtil.getCivilStatusInAllLanguages(CivilStatus.DIVORCED));
        civilStatus.put(CivilStatus.WIDOWED, CivilStatusUtil.getCivilStatusInAllLanguages(CivilStatus.WIDOWED));
        civilStatus.put(CivilStatus.ANNULLED, CivilStatusUtil.getCivilStatusInAllLanguages(CivilStatus.ANNULLED));
        return civilStatus;
    }
}
